<?php
class Api_Lib_Rsp_Kuwei {

    function __construct() {
        $this->_modelExample = FLEA::getSingleton('Model_Jichu_Kuwei');
    }

    /**
     *
     * Time：2019/11/07 14:01:47
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function updateKuwei($params,& $service){
        if(!$params['kuweiName']){
            $service->send_user_error('kuweiName required');
        }
        //检查是否存在
        $Kuwei = $this->_modelExample->find(array('kuweiName'=>$params['kuweiName']));
        //如果本地没有找到，则判断是否重复
        if(!$Kuwei['id']) {
            $condition = array();
            $condition[] = array('kuweiName',$params['kuweiName'],'=');
            $count = $this->_modelExample->findCount($condition);

            if($count > 0) {
                $service->send_user_error('kuweiName repeat');
            }
        } else {
            //修改时判断是否重复
            $condition = array();
            $condition[] = array('kuweiName',$params['kuweiName'],'=');
            $condition[] = array('id',$Kuwei['id'],'<>');
            $count = $this->_modelExample->findCount($condition);

            if($count > 0) {
                $service->send_user_error('kuweiName repeat');
            }
        }


        $_data = array(
            'id'        => $Kuwei['id']+0,
            'kuweiName' => $params['kuweiName'],
            'memo'      => $params['memo'],
        );

        //数据保存
        $result = $this->_modelExample->save($_data);

        return array('success'=>$result ? true :false ,'msg'=>'完成');
    }


    /**
     * 获取库位列表
     * Time：2019/11/07 15:09:10
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function getList($params,& $service){
        $pageNum     = $params['pageNum'] ? $params['pageNum'] : 20;
        $currentPage = $params['page'] ? $params['page'] : 1;

        $condition = array();
        if($params['kuweiName']!='') {
            $condition[] = array('kuweiName',"%{$params['kuweiName']}%",'like');
        }
        if($params['key']!='') {
            $condition[] = array('kuweiName',"%{$params['key']}%",'like');
        }

        // $this->_modelExample->clearLinks();
        FLEA::loadClass('TMIS_Pager');
        $pager = & new TMIS_Pager($this->_modelExample,$condition,'id desc',$pageNum ,($currentPage - 1));
        $rowset = $pager->findAll();
        //把需要的数据带出来
        $rows = array();
        foreach ($rowset as $key => & $v) {
            $rows[] = array(
                'id'        => $v['id'],
                'kuweiName' => $v['kuweiName'],
                'memo'      => $v['memo'],
            );
        }

        return array('row'=>$rows,'page'=>$currentPage,'total'=>$pager->totalCount,'pageCount'=>count($rows));
    }
}
?>